<?php
class Mysql extends DAO {

  /**
   * Mysql::modifyLimitQuery()
   * Adds an adapter-specific LIMIT clause to the SELECT statement.
   *
   * @param string $query
   * @param mixed $limit
   * @param mixed $offset
   * @return string
   */
  public function modifyLimitQuery($query, $limit = false, $offset = false, $isManip = false) {
    $limit = intval($limit);
    $offset = intval($offset);

    if($offset < 0) {
      throw new PDOException("LIMIT argument offset=$offset is not valid");
    }
    if ($limit) {
      if ($offset) {
        $query .= "
				LIMIT $offset, $limit
          ";
      }
      else {
        $query .= "
				LIMIT $limit
          ";
      }
    }

    return $query;
  }

  /**
   * Mysql::replace()
   *
   * @param mixed $tablename
   * @param mixed $values
   * @return
   */
  public function replacevalues($tablename, $values) {
    $query = "REPLACE INTO ".$tablename." VALUES (".$values.")";
    return parent::query($query);
  }

  /**
   * Mysql::replace()
   *
   * @param mixed $tablename
   * @param mixed $champ
   * @param mixed $value
   * @return
   */
  public function replace($tablename, $champ, $value) {
    $query = "REPLACE INTO ".$tablename." (".$champ.") VALUES (".$value.")";
    return parent::query($query);
  }

  /**
   * Mysql::dateFRToMySql()
   * Transforme une date Fran�aise au format de date MySQL
   *
   * @param mixed $dateFR
   * @return void
   */
  public function dateFrToMySql($dateFr) {
    if(Verif::checkDateFr($dateFr, '/')) {
      return Date::dateFrToUs($dateFr,'/','-');
    }
  }

  /**
   * Mysql::inserts()
   * Fonction permettant de faire des inserts multiples en une seule requete SQL
   *
   * @param mixed $tablename
   * @param mixed $champ
   * @param mixed $value
   * @return
   */
  public function inserts($tablename, $champ, $values) {
    $query = "INSERT INTO ".$tablename." (".$champ.") VALUES ".$values;

    return $this->query($query);
  }


  /**
   * Mysql::vaChercher()
   *
   * @param mixed $tablename
   * @param mixed $champ_affiche
   * @param mixed $champ_recherche
   * @param mixed $recherche
   * @param string $where
   * @param string $tri
   * @param bool $limit
   * @param bool $offset
   * @return
   */
  public function vaChercher($tablename, $champ_affiche, $champ_recherche, $recherche, $where="", $tri="", $limit = false, $offset = false) {
    $sql = "SELECT ".$champ_affiche.", MATCH(".$champ_recherche.") AGAINST(".$recherche.") AS cpt FROM ".$tablename." WHERE MATCH(".$champ_recherche.") AGAINST (".$recherche.")";

    if($where != "") {
      $sql.= " AND ".$where." ".$tri." ORDER BY cpt DESC";
    }
    else {
      $sql.= " ".$tri." ORDER BY cpt DESC";
    }

    if($limit != false) {
      $sql = modifyLimitQuery($sql, $limit, $offset);
    }

    return parent::query($sql);
  }

  /**
   * Mysql::countChercher()
   *
   * @param mixed $tablename
   * @param mixed $champ_affiche
   * @param mixed $champ_recherche
   * @param mixed $recherche
   * @param string $where
   * @param string $tri
   * @param bool $limit
   * @param bool $offset
   * @return
   */
  public function countChercher($tablename, $champ_affiche, $champ_recherche, $recherche, $where="", $tri="", $limit = false, $offset = false) {
    $sql = "SELECT COUNT(*), ".$champ_affiche." FROM ".$tablename." WHERE MATCH(".$champ_recherche.") AGAINST (".$recherche.")";

    if($where != "") {
      $sql.= " AND ".$where." ".$tri;
    }
    else {
      $sql.= " ".$tri;
    }

    if($limit != false) {
      $sql = modifyLimitQuery($sql, $limit, $offset);
    }

    return parent::query($sql);

  }

}

?>